from fastapi import APIRouter,Depends
from sqlalchemy import desc,asc,and_
from model.downhole_sensor import GroundSensor
from model.downhole_mine import GroundMine
from sqlalchemy.orm import Session
from db.mysql import get_db
from vendor.extend.courier import *
from vendor.extend.conversion import is_positive_integer


V3DataSensor = APIRouter()


@V3DataSensor.get('/getlist')
async def getlist(colliery_id:int=0,db: Session = Depends(get_db)):
    if not is_positive_integer(colliery_id):
        return Error(msg='矿井ID错误')

    query = db.query(GroundSensor, GroundMine).join(GroundSensor, GroundSensor.mine_id == GroundMine.id)
    results = query.filter(GroundMine.colliery_id==colliery_id).order_by(GroundSensor.id.desc()).all()
    list_data=[]
    for sensor,mine in results:
        list_data.append({
            "colliery_id":mine.colliery_id,
            "sensor_id":sensor.id,
            "mine_id":mine.id,
            "sensor_number":sensor.sensor_number,
            "mine_serial_number":mine.serial_number,
            "mine_distance":mine.distance,
            "mine_x":mine.x,
            "mine_y":mine.y,
            "mine_z":mine.z,
            "mine_modem":mine.modem,
            "sensor_setup_time":sensor.setup_time,
            "sensor_chanel":sensor.chanel,
        })
    return Success(data=list_data)
